package com.live.stamp.web.message.service.impl;

import com.live.stamp.base.service.impl.BaseSupportServiceImpl;
import com.live.stamp.base.token.UserModelUtil;
import com.live.stamp.base.utils.UUIDUtils;
import com.live.stamp.global.commonenum.ListTypeEnum;
import com.live.stamp.global.commonenum.MessageTypeEnum;
import com.live.stamp.web.message.baseDataService.MessageBaseService;
import com.live.stamp.web.message.model.TMessage;
import com.live.stamp.web.message.service.MessageService;
import com.live.stamp.web.message.vo.TMessageVo;
import com.live.stamp.web.stamp.baseDataService.StampDataService;
import com.live.stamp.web.stamp.model.TStampComment;
import com.live.stamp.web.stamp.vo.TStampInfoVo;
import com.live.stamp.web.user.baseDataService.UserBaseService;
import com.live.stamp.web.user.model.TUser;
import com.live.stamp.web.user.service.UserService;
import com.live.stamp.web.user.vo.TUserVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.boot.container.core.common.BusinessException;
import org.springframework.boot.container.core.log.ZKLogger;
import org.springframework.boot.container.core.log.ZKLoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * @author cwd E-mail: wendong.cao@jsxfedu.com
 * @version 1
 * @date 2018/5/12 15:57
 */
@Service
public class MessageServiceImpl extends BaseSupportServiceImpl implements MessageService {
    private static final ZKLogger logger = ZKLoggerFactory.getLogger(MessageServiceImpl.class);

    @Resource
    private MessageBaseService messageBaseService;
    @Resource
    private UserService userService;
    @Resource
    private UserBaseService userBaseService;

    @Resource
    StampDataService stampDataService;

    @Override
    public void addMessage(TMessage tMessage) throws BusinessException {
        logger.debug("enter method addMessage, param tMessage:{}", tMessage);

        tMessage.setId(UUIDUtils.getUUID());

        messageBaseService.addMessage(tMessage);
    }

    @Override
    public List<TMessage> messageList(TMessage tMessage) throws BusinessException {
        return messageBaseService.messageList(tMessage);
    }

    @Override
    public TMessage getOneMessage(TMessage tMessage) throws BusinessException {

        tMessage = messageBaseService.getOneMessage(tMessage);

        tMessage.setState("2");         //设置消息为已读
        messageBaseService.updateMessage(tMessage);

        return tMessage;
    }

    @Override
    public void sendCommonStampMessage(TStampComment tStampComment) throws BusinessException {
        logger.debug("enter method sendCommonStampMessage, param tStampComment:{}", tStampComment);

        if(tStampComment != null) {
            List<Map> commentList = stampDataService.listCommentByStampId(tStampComment.getTInjiInfoId());    //评论列表

            TUser tUser = userBaseService.getUserById(tStampComment.getTUserId());
            if(tUser != null) {
                List<String> sendList = new ArrayList<>();

                TMessage tMessage = new TMessage();

                TStampInfoVo tStampInfoVo = new TStampInfoVo();
                tStampInfoVo.setId(tStampComment.getTInjiInfoId());
                tStampInfoVo = stampDataService.getStampInfo(tStampInfoVo);

                tMessage.setBusiId(tStampComment.getTInjiInfoId());
                tMessage.setContent(tStampComment.getComment());
                tMessage.setCreateTime(new Date());
                tMessage.setState("1");
                tMessage.setType(MessageTypeEnum.COMM.name());
                tMessage.setFromUserId(tUser.getId());
                tMessage.setFromUserName(tUser.getUserName());
                tMessage.setFromUserPhoto(tUser.getPhotoUrl());
                tMessage.setToUserId(tStampInfoVo.getTUserId());
                if(!StringUtils.equals(tStampInfoVo.getTUserId(), tUser.getId())) {     //给创建者发送消息提醒
                    addMessage(tMessage);
                }

                sendList.add(tUser.getId());
                sendList.add(tStampInfoVo.getTUserId());
                if(commentList != null) {           //给参与评论的相关人员发送消息提醒
                    String userId;
                    for (Map data : commentList) {
                        userId = data.get("tUserId").toString();
                        if(!sendList.contains(userId)) {
                            tMessage.setContent(tStampComment.getComment());
                            tMessage.setToUserId(userId);
                            addMessage(tMessage);

                            sendList.add(userId);
                        }
                    }
                }
            }
        }
    }

    @Override
    public void sendAttenMessage(TUserVo tUserVo) throws BusinessException {
        logger.debug("enter method sendAttenMessage, param tUserVo:{}", tUserVo);

        TUser tUser = UserModelUtil.getUserModel();
        if(tUser != null) {
            TMessage tMessage = new TMessage();          //给创建者发送消息提醒
            tMessage.setBusiId(tUserVo.getId());
            tMessage.setContent("好友关注");
            tMessage.setType(MessageTypeEnum.ATTEN.name());
            tMessage.setCreateTime(new Date());
            tMessage.setState("1");
            tMessage.setFromUserId(tUser.getId());
            tMessage.setFromUserName(tUser.getUserName());
            tMessage.setFromUserPhoto(tUser.getPhotoUrl());
            tMessage.setToUserId(tUserVo.getUserId());
            addMessage(tMessage);
        }

    }

    @Override
    public String isHaveNewMessage() throws BusinessException {
        TUser tUser = UserModelUtil.getUserModel();

        TMessage tMessage = new TMessage();
        tMessage.setToUserId(tUser.getId());
        tMessage.setType(MessageTypeEnum.COMM.name());
        tMessage.setState("1");     //未读

        List<TMessage> messages = messageList(tMessage);            //是否有未读评论
        if(messages!=null && messages.size()>0) {
            return "1";
        }

        tMessage.setType(MessageTypeEnum.ATTEN.name());             //是否有未读新关注
        messages = messageList(tMessage);
        if(messages!=null && messages.size()>0) {
            return "1";
        }

        TUserVo tUserVo = new TUserVo();
        tUserVo.setState("2");   //状态：0 删除、1 已添加 、2 请求、3 已拒绝
        tUserVo.setListType(ListTypeEnum.FRIEND.toString());
        List<TUserVo> userVos = userService.amityList(tUserVo);
        if(userVos!=null && userVos.size()>0) {
            return "1";
        }

        return "0";
    }

    @Override
    public Map<String, Object> homePageMessage() throws BusinessException {
        Map<String, Object> data = new HashMap<>();

        TUser tUser = UserModelUtil.getUserModel();

        TMessage tMessage = new TMessage();
        tMessage.setToUserId(tUser.getId());
        tMessage.setType(MessageTypeEnum.COMM.name());
        tMessage.setState("1");     //未读

        List<TMessage> messages = messageList(tMessage);            //是否有未读评论
        if(messages!=null && messages.size()>0) {
            data.put("commonMessage", "1");
        } else {
            data.put("commonMessage", "0");
        }

        tMessage.setType(MessageTypeEnum.ATTEN.name());             //是否有未读新关注
        messages = messageList(tMessage);
        if(messages!=null && messages.size()>0) {
            data.put("attenMessage", "1");
        } else {
            data.put("attenMessage", "0");
        }

        TUserVo tUserVo = new TUserVo();
        tUserVo.setState("2");   //状态：0 删除、1 已添加 、2 请求、3 已拒绝
        tUserVo.setListType(ListTypeEnum.FRIEND.toString());
        List<TUserVo> userVos = userService.amityList(tUserVo);
        if(userVos!=null && userVos.size()>0) {
            data.put("friendMessage", "1");
        } else {
            data.put("friendMessage", "0");
        }

        return data;
    }

    /**
     * 获取评论消息
     * @param tMessage
     * @return
     */
    @Override
    public List<TMessageVo> getCommonMessageList(TMessage tMessage) {
        TUser tUser = UserModelUtil.getUserModel();
        tMessage.setType(MessageTypeEnum.COMM.name());
        tMessage.setToUserId(tUser.getId());

        return messageBaseService.commonMessageList(tMessage);
    }

    @Override
    public List<TMessage> getAttenMessageList(TMessage tMessage) throws BusinessException {
        TUser tUser = UserModelUtil.getUserModel();
        tMessage.setType(MessageTypeEnum.ATTEN.name());
        tMessage.setState("1");     //未读
        tMessage.setToUserId(tUser.getId());

        List<TMessage> messages = messageBaseService.messageList(tMessage);

        messageBaseService.updateAttenMessageRead(tUser.getId());

        return messages;
    }
}
